<div xmlns="http://www.w3.org/1999/xhtml"
     xmlns:py="http://genshi.edgewall.org/"
     xmlns:i18n="http://genshi.edgewall.org/i18n"
     py:strip="">

  <!--! Display check items
  -
  -      'checkitems'   check items to be shown
  -->
  <py:def function="show_checkitems(checkitems)">
    <h3 class="comment:description">Check Item${len(checkitems) > 1 and 's' or ''}</h3>
    <div py:if="len(checkitems) > 0" class="searchable" xml:space="preserve">
      <table id="checkitems">
        <thead>
          <th class="check_id">Check ID</th>
          <th class="check_point">Check Point</th>
        </thead>
        <tbody>
          <py:for each="checkitem in checkitems">
          <tr>
            <td class="check_id">$checkitem.check_id</td>
            <td class="check_point">$checkitem.check_point</td>
          </tr>
          </py:for>
        </tbody>
      </table>
    </div>
    <div py:if="len(checkitems) == 0">No check item</div>
  </py:def>

  <!--! Display check items
  -
  -      'checkitems'   check items to be shown
  -->
  <py:def function="show_checkitems_form(checkitems)">
    <script type="text/javascript">/*<![CDATA[*/
      var checkitem_id = ${len(checkitems)};
      function deleteCheckItem(id) {
	  $("#" + id).remove();
      }
      function addCheckItem() {
	  var id = "checkitem_row_" + checkitem_id;
          var row = $("<tr>");
	  var td0 = $("<td>");
	  var td1 = $("<td>");
	  var td2 = $("<td>");
          var input0 = $("<input>").addClass("check_id");
          var input1 = $("<input>").addClass("check_point");
	  var dellink = $("<a>").addClass("dellink");
	  input0.attr("name", "check_id_" + checkitem_id)
          input0.attr("type", "text").attr("size", "4");
	  input0.attr("value", "" + (checkitem_id + 1));
	  input1.attr("name", "check_point_" + checkitem_id)
          input1.attr("type", "text");
	  dellink.attr("onClick", "deleteCheckItem('" + id + "')");
	  dellink.attr("href", "#").html("delete");
	  td0.append(input0);
	  td1.append(input1);
	  td2.append(dellink);
	  row.attr("id", id);
	  row.append(td0).append(td1).append(td2);
          $("#checkitem_table").append(row);
          checkitem_id++;
      }
    /*]]>*/</script>
    <table id="checkitem_table">
      <thead>
        <th class="check_id">Check ID</th>
        <th class="check_point">Check Point</th>
        <th class="action">Action</th>
      </thead>
      <tbody>
        <py:for each="n, checkitem in enumerate(checkitems)">
        <input py:if="checkitem.id" type="hidden"
               name="${'checkitem_id_%i' % n}" value="$checkitem.id"/>
        <tr id="${'checkitem_row_%i' % n}">
          <td>
            <input class="check_id" type="text" size="4"
                   name="${'check_id_%i' % n}"
                   value="$checkitem.check_id"/>
          </td>
          <td>
            <input class="check_point" type="text"
                   name="${'check_point_%i' % n}"
                   value="$checkitem.check_point"/>
          </td>
          <td>
            <a href="#" onClick="deleteCheckItem('${'checkitem_row_%i' % n}')">delete</a>
          </td>
        </tr>
        </py:for>
      </tbody>
    </table>
    <a href="#" onClick="addCheckItem()">add</a>
  </py:def>

  <!--! Display a project
  -
  -      'project'      a project to be shown
  -      'preview_mode' True or False
  -->
  <py:def function="show_project(project, customfields, preview_mode=False)">
    <div id="project" class="${preview_mode and 'draft' or None}">
      <div class="date">
        <p i18n:msg="created" py:if="project.node.id">Created ${dateinfo(project.node.created)} ago</p>
        <p i18n:msg="modified" py:if="project.node.created != project.node.modified">Last modified ${dateinfo(project.node.modified)} ago</p>
        <p py:if="not project.node.id"><i>(project not yet created)</i></p>
      </div>
      <h2 class="searchable project_name">$project.node.name</h2>
      ${show_customfields(customfields, table=True)}
      <h3 class="comment:description">Description</h3>
      <div py:if="project.node.description" class="searchable" xml:space="preserve">
        ${wiki_to_html(context, project.node.description)}
      </div>
      ${show_customfields(customfields, table=False)}
    </div>
  </py:def>

  <!--!  Display a project form
  -
  -      'project'      a project to be shown
  -      'customfields' custom fields
  -->
  <py:def function="show_project_form(project, customfields)">
    <fieldset id="properties">
      <legend>${project.node and 'Change ' or ''}Properties</legend>
      <table>
        <tbody>
        <tr>
          <th><label for="field-name">Project Name:</label></th>
          <td class="fullrow">
            <input type="text" id="field-name" name="name" size="64"
                   value="$project.node.name" />
          </td>
        </tr>
        <tr>
          <th><label for="field-description">Description:</label></th>
          <td class="fullrow">
            <textarea id="field-description" name="description"
                      class="wikitext trac-resizable" rows="16" cols="68"
                      >${project.node.description}</textarea>
          </td>
        </tr>
        <!-- The custom fields are to be shown -->
        <py:for each="field, value in customfields">
          ${show_customfield_form(field, value)}
        </py:for>
        </tbody>
      </table>
    </fieldset>
  </py:def>

  <!--!  Display a test suite
  -
  -      'testsuite'    a testsuite to be shown
  -      'customfields' custom fields
  -      'preview_mode' True or False
  -->
  <py:def function="show_testsuite(testsuite, customfields=None, preview_mode=False)">
    <div id="testsuite" class="${preview_mode and 'draft' or None}">
      <div class="date">
        <p i18n:msg="created" py:if="testsuite.node.id">Created ${dateinfo(testsuite.node.created)} ago</p>
        <p i18n:msg="modified" py:if="testsuite.node.created != testsuite.node.modified">Last modified ${dateinfo(testsuite.node.modified)} ago</p>
        <p py:if="not testsuite.node.id"><i>(test suite not yet created)</i></p>
      </div>
      <h2 class="searchable testsuite_name">$testsuite.node.name</h2>
      ${show_customfields(customfields, table=True)}
      <h3 class="comment:description">Description</h3>
      <div py:if="testsuite.node.description" class="searchable" xml:space="preserve">
        ${wiki_to_html(context, testsuite.node.description)}
      </div>
      ${show_customfields(customfields, table=False)}
    </div>
  </py:def>

  <!--!  Display a test suite form
  -
  -      'testsuite'    a test suite to be shown
  -      'customfields' custom fields
  -->
  <py:def function="show_testsuite_form(testsuite, customfields)">
    <fieldset id="properties">
      <input type="hidden" name="parent_id" value="$testsuite.node.parent_id" />
      <input type="hidden" name="id" value="$testsuite.node.id" />
      <legend>${testsuite.node.id and 'Change ' or ''}Properties</legend>
      <table>
        <tbody>
        <tr>
          <th><label for="field-name">Test Suite Name:</label></th>
          <td class="fullrow">
            <input type="text" id="field-name" name="name" size="64"
                   value="$testsuite.node.name" />
          </td>
        </tr>
        <tr>
          <th><label for="field-description">Description:</label></th>
          <td class="fullrow">
            <textarea id="field-description" name="description"
                      class="wikitext trac-resizable" rows="10" cols="64"
                      >${testsuite.node.description}</textarea>
          </td>
        </tr>
        <!-- The custom fields are to be shown -->
        <py:for each="field, value in customfields">
          ${show_customfield_form(field, value)}
        </py:for>
        </tbody>
      </table>
    </fieldset>
  </py:def>

  <!--!  Display a test case
  -
  -      'testcase'     a test case to be shown
  -      'customfields' custom fields
  -      'preview_mode' True or False
  -->
  <py:def function="show_testcase(testcase, customfields, preview_mode=False)">
    <div id="testcase" class="${preview_mode and 'draft' or None}">
      <div class="date">
        <p i18n:msg="created" py:if="testcase.node.id">Created ${dateinfo(testcase.node.created)} ago</p>
        <p i18n:msg="modified" py:if="testcase.node.created != testcase.node.modified">Last modified ${dateinfo(testcase.node.modified)} ago</p>
        <p py:if="not testcase.node.id"><i>(test case not yet created)</i></p>
      </div>
      <h2 class="searchable testcase_name">$testcase.node.name</h2>
      ${show_customfields(customfields, table=True)}
      <h3 class="comment:description">Description</h3>
      <div py:if="testcase.node.description" class="searchable" xml:space="preserve">
        ${wiki_to_html(context, testcase.node.description)}
      </div>
      ${show_checkitems(testcase.node.checkitems)}
      ${show_customfields(customfields, table=False)}
    </div>
  </py:def>

  <!--!  Display a test case form
  -
  -      'testcase'     a test case to be shown
  -      'customfields' custom fields
  -->
  <py:def function="show_testcase_form(testcase, cutomfields, checkitem=True)">
    <fieldset id="properties">
      <table>
        <tbody>
        <tr>
          <th><label for="field-name">Test Case Name:</label></th>
          <td class="fullrow">
            <input type="text" id="field-name" name="name" size="64"
                   value="$testcase.node.name" />
          </td>
        </tr>
        <tr>
          <th><label for="field-description">Description:</label></th>
          <td class="fullrow">
            <textarea id="field-description" name="description"
                      class="wikitext trac-resizable" rows="10" cols="64"
                      >${testcase.node.description}</textarea>
          </td>
        </tr>
        <!-- The check items are to be shown -->
        <tr>
          <th><label>Check Item(s):</label></th>
          <td class="fullrow">
            ${show_checkitems_form(testcase.node.checkitems)}
          </td>
        </tr>
        <!-- The custom fields are to be shown -->
        <py:for each="field, value in customfields">
          ${show_customfield_form(field, value)}
        </py:for>
        </tbody>
      </table>
      <input type="hidden" name="parent_id" value="$testcase.node.parent_id" />
      <input type="hidden" name="id" value="$testcase.node.id" />
      <legend>${testcase.node.id and 'Change ' or ''}Properties</legend>
    </fieldset>
  </py:def>

  <!--!  Display a custom field
  -
  -      'field' a test case to be shown
  -      'value' 
  -->
  <py:def function="show_customfield(field, value)">
    <h3 class="comment:description">$field.label</h3>
    <div class="searchable" xml:space="preserve">
      <py:choose test="value.value and value.value.startswith('#!') or False">
        <py:when test="True">${wiki_to_html(context, '{{{\n' + value.value + '\n}}}')}</py:when>
        <py:otherwise><pre>$value.value</pre></py:otherwise>
      </py:choose>
    </div>
    <!-- [$field.label] $value.value -->
  </py:def>

  <!--! Display custom fields
  -
  -     'customfields'
  -->
  <py:def function="show_customfields(customfields, table=True)">
    <py:if test="table">
      <!-- Table -->
      <py:if test="len([f for f, v in customfields if get_field_type_name(f.field_type) != 'Text Area']) > 0">
        <table id="customfield" class="properties">
          <tbody>
          <py:for each="field, value in customfields">
            <py:with vars="type_name = get_field_type_name(field.field_type)">
              <py:if test="type_name != 'Text Area'">
                <tr>
                  <th>$field.label:</th>
                  <td>
                    <py:choose test="type_name">
                      <py:when test="'Checkbox'">
                        <input type="checkbox" checked="${value and value.value.lower() in ('on', '1', 'yes', 'checked') and 'checked' or None}" disabled="disabled" />
                      </py:when>
                      <py:otherwise>$value.value</py:otherwise>
                    </py:choose>
                  </td>
                </tr>
              </py:if>
            </py:with>
          </py:for>
          </tbody>
        </table>
      </py:if>
    </py:if>
    <py:if test="not table">
      <!-- Text Area -->
      <py:for each="field, value in customfields">
        <py:if test="get_field_type_name(field.field_type) == 'Text Area'">
          <h3 class="comment:description">$field.label</h3>
          <div class="searchable" xml:space="preserve">
            <py:choose test="value.value and value.value.startswith('#!') or False">
              <py:when test="True">${wiki_to_html(context, '{{{\n' + value.value + '\n}}}')}</py:when>
              <py:otherwise><pre>$value.value</pre></py:otherwise>
            </py:choose>
          </div>
        </py:if>
      </py:for>
    </py:if>
  </py:def>

  <!--!  Display a custom field form
  -
  -      'field' a test case to be shown
  -      'value' 
  -->
  <py:def function="show_customfield_form(field, value)">

    <py:with vars="default = value.value if value else field.default_value">
    <py:with vars="field_id = 'customfield_%i' % field.id">
      <tr>
        <th><label for="$field_id">$field.label:</label></th>
        <td class="fullrow">
          <py:choose test="get_field_type_name(field.field_type)">
            <input py:when="'String'" type="text" size="64"
                   id="$field_id" name="$field_id" value="$default" />
            <textarea py:when="'Text Area'" id="$field_id" name="$field_id"
                      class="wikitext trac-resizable" rows="8" cols="64"
                      >$default</textarea>
            <input py:when="'Checkbox'" type="checkbox" id="$field_id" name="$field_id"
                   checked="${default.lower() in ('on', '1', 'yes', 'checked') and 'checked' or None}" />
            <input py:when="'Date'" type="text" size="30"
                   id="$field_id" name="$field_id" value="$default" />
            <select py:when="'List'" id="$field_id" name="$field_id">
              <py:for each="option in [x.strip() for x in (',' + field.possible_values).split(',')]">
                <option value="$option" selected="${default == option and 'selected' or None}">$option</option>
              </py:for>
            </select>
            <select py:when="'Multiple Select List'" id="$field_id" name="$field_id" size="3" multiple="multiple">
              <py:with vars="values = [x.strip() for x in default.split(',')]">
                <py:for each="option in [x.strip() for x in field.possible_values.split(',')]">
                  <option value="$option" selected="${(option in values) and 'selected' or None}">$option</option>
                </py:for>
              </py:with>
            </select>
          </py:choose>
        </td>
      </tr>
    </py:with>
    </py:with>
  </py:def>

</div>

